package com.fr.report.script.function;

import com.fr.base.FRContext;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.util.Utils;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LEFT extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    Object localObject2;
    if (paramArrayOfObject.length < 1)
      return Primitive.ERROR_NAME;
    Object localObject1 = paramArrayOfObject[0];
    int i = 1;
    if (paramArrayOfObject.length > 1)
    {
      localObject2 = paramArrayOfObject[1];
      if (localObject2 instanceof Number)
        i = ((Number)localObject2).intValue();
      else
        try
        {
          i = Integer.parseInt(localObject2.toString());
        }
        catch (Exception localException2)
        {
          FRContext.getLogger().log(Level.WARNING, localException2.getMessage(), localException2);
        }
    }
    try
    {
      localObject2 = Utils.objectToString(localObject1);
      return ((String)localObject2).substring(0, Math.max(0, Math.min(i, ((String)localObject2).length())));
    }
    catch (Exception localException1)
    {
    }
    return "";
  }

  public Function.Type getType()
  {
    return Function.TEXT;
  }

  public String getCN()
  {
    return "LEFT(text,num_chars): 根据指定的字符数返回文本串中的第一个或前几个字符。\nText:包含需要选取字符的文本串或单元格引用。\nNum_chars:指定返回的字符串长度。\n备注:\n    Num_chars的值必须等于或大于0。\n    如果num_chars大于整个文本的长度，LEFT函数将返回所有的文本。\n    如果省略num_chars，则默认值为1。\n示例:\nLEFT(\"Fine software\",8)等于“Fine sof”。\nLEFT(\"Fine software\")等于“F”。\n如果单元格A3中含有“China”，则LEFT(A3,2)等于“Ch”。";
  }

  public String getEN()
  {
    return "LEFT(text,num_chars): LEFT returns the first character or characters in a text string, based on the number of characters you specify.\nText is the text string that contains the characters you want to extract.\nNum_chars specifies the number of characters you want LEFT to extract.\n\nRemarks:\n1. Num_chars must be greater than or equal to zero.\n2. If num_chars is greater than the length of text, LEFT returns all of text.\n3. If num_chars is omitted, it is assumed to be 1.\n\nExample:\n   LEFT(\"Fine software\",8) = \"Fine sof\"\n   LEFT(\"Fine software\") = \"F\"\n   If value of cell A3 is \"China\", then LEFT(A3,2) = \"Ch\".";
  }
}